package cn.ivicar.iov.service.impl;

import cn.ivicar.iov.dao.FlowPackageDao;
import cn.ivicar.iov.dao.FlowPackageHistoryDao;
import cn.ivicar.iov.dao.IdentifyDao;
import cn.ivicar.iov.dao.VehicleDao;
import cn.ivicar.iov.entity.FlowPackageHistory;
import cn.ivicar.iov.service.FlowPackageHistoryService;
import cn.ivicar.iov.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: iov
 * @description: ${description}
 * @author: Mr.Wang
 * @create: 2018-08-15 14:28
 **/
@Service
public class FlowPackageHistoryServiceImpl implements FlowPackageHistoryService{
    @Autowired
    private FlowPackageHistoryDao flowPackageHistoryDao;
    @Autowired
    private IdentifyDao identifyDao;
    @Autowired
    private FlowPackageDao flowPackageDao;
    @Autowired
    private VehicleDao vehicleDao;


    @Override
    public List<FlowPackageHistory> getHistoryPackage(String vehicle_serial) {
        return flowPackageHistoryDao.queryHistoryPackage(identifyDao.queryByVehicleId(vehicleDao.queryVehicleId(vehicle_serial)));
    }

    @Override
    public boolean addHistoryPackage(String vehicle_serial, int flow_package_id) {
        DateUtil dateUtil = new DateUtil();
        FlowPackageHistory flowPackageHistory = new FlowPackageHistory();
        flowPackageHistory.setIccid(identifyDao.queryByVehicleId(vehicleDao.queryVehicleId(vehicle_serial)));
        if(flowPackageHistory.getIccid()!= null && !"".equals(flowPackageHistory.getIccid())){
            flowPackageHistory.setFlow_package_id(flow_package_id);
            flowPackageHistory.setPaytime(new Date());
            flowPackageHistory.setFlow_amount(flowPackageDao.queryFlowAmountByFlowPackageId(flow_package_id));
            flowPackageHistory.setStart_time(dateUtil.getStartTime());
            flowPackageHistory.setEnd_time(dateUtil.getEndTime(flowPackageDao.queryDurationByFlowPackageId(flow_package_id)));
            flowPackageHistory.setExpense(flowPackageDao.queryExpenseByFlowPackageId(flow_package_id));
            try{
                int effectNum = flowPackageHistoryDao.insertHistoryPackage(flowPackageHistory);
                if(effectNum>0){
                    return true;
                }
                else{
                    throw new RuntimeException("插入信息失败");
                }
            }catch (Exception e){
                throw new RuntimeException("插入信息失败");
            }
        }
        else{
            throw new RuntimeException("iccid信息不能为空！");
        }

    }


}
